﻿
using MySql.Data.MySqlClient;
/**  版本信息模板在安装目录下，可自行修改。
* picalbum.cs
*
* 功 能： N/A
* 类 名： picalbum
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2016/9/29 星期四 17:14:25   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;

using Utilities;//Please add references
namespace Beauty.DAL
{
    /// <summary>
    /// 数据访问类:picalbum
    /// </summary>
    public partial class picalbum
    {
        public picalbum()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("AlbumId", "picalbum");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int AlbumId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from picalbum");
            strSql.Append(" where AlbumId=@AlbumId");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@AlbumId", MySqlDbType.Int32)
            };
            parameters[0].Value = AlbumId;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Beauty.Model.picalbum model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into picalbum(");
            strSql.Append("Title,Description,AddTime,UpdateTime,CoverId,ClassId,UserId,SortId)");
            strSql.Append(" values (");
            strSql.Append("@Title,@Description,@AddTime,@UpdateTime,@CoverId,@ClassId,@UserId,@SortId)");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Title", MySqlDbType.VarChar,255),
                    new MySqlParameter("@Description", MySqlDbType.VarChar,255),
                    new MySqlParameter("@AddTime", MySqlDbType.DateTime),
                    new MySqlParameter("@UpdateTime", MySqlDbType.DateTime),
                    new MySqlParameter("@CoverId", MySqlDbType.Int32,11),
                    new MySqlParameter("@ClassId", MySqlDbType.Int32,11),
                    new MySqlParameter("@UserId", MySqlDbType.Int32,11),
                    new MySqlParameter("@SortId", MySqlDbType.Int32,11)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.Description;
            parameters[2].Value = model.AddTime;
            parameters[3].Value = model.UpdateTime;
            parameters[4].Value = model.CoverId;
            parameters[5].Value = model.ClassId;
            parameters[6].Value = model.UserId;
            parameters[7].Value = model.SortId;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Beauty.Model.picalbum model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update picalbum set ");
            strSql.Append("Title=@Title,");
            strSql.Append("Description=@Description,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("UpdateTime=@UpdateTime,");
            strSql.Append("CoverId=@CoverId,");
            strSql.Append("ClassId=@ClassId,");
            strSql.Append("UserId=@UserId,");
            strSql.Append("SortId=@SortId");
            strSql.Append(" where AlbumId=@AlbumId");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Title", MySqlDbType.VarChar,255),
                    new MySqlParameter("@Description", MySqlDbType.VarChar,255),
                    new MySqlParameter("@AddTime", MySqlDbType.DateTime),
                    new MySqlParameter("@UpdateTime", MySqlDbType.DateTime),
                    new MySqlParameter("@CoverId", MySqlDbType.Int32,11),
                    new MySqlParameter("@ClassId", MySqlDbType.Int32,11),
                    new MySqlParameter("@UserId", MySqlDbType.Int32,11),
                    new MySqlParameter("@SortId", MySqlDbType.Int32,11),
                    new MySqlParameter("@AlbumId", MySqlDbType.Int32,11)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.Description;
            parameters[2].Value = model.AddTime;
            parameters[3].Value = model.UpdateTime;
            parameters[4].Value = model.CoverId;
            parameters[5].Value = model.ClassId;
            parameters[6].Value = model.UserId;
            parameters[7].Value = model.SortId;
            parameters[8].Value = model.AlbumId;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int AlbumId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from picalbum ");
            strSql.Append(" where AlbumId=@AlbumId");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@AlbumId", MySqlDbType.Int32)
            };
            parameters[0].Value = AlbumId;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string AlbumIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from picalbum ");
            strSql.Append(" where AlbumId in (" + AlbumIdlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Beauty.Model.picalbum GetModel(int AlbumId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AlbumId,Title,Description,AddTime,UpdateTime,CoverId,ClassId,UserId,SortId from picalbum ");
            strSql.Append(" where AlbumId=@AlbumId");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@AlbumId", MySqlDbType.Int32)
            };
            parameters[0].Value = AlbumId;

            Beauty.Model.picalbum model = new Beauty.Model.picalbum();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Beauty.Model.picalbum DataRowToModel(DataRow row)
        {
            Beauty.Model.picalbum model = new Beauty.Model.picalbum();
            if (row != null)
            {
                if (row["AlbumId"] != null && row["AlbumId"].ToString() != "")
                {
                    model.AlbumId = int.Parse(row["AlbumId"].ToString());
                }
                if (row["Title"] != null)
                {
                    model.Title = row["Title"].ToString();
                }
                if (row["Description"] != null)
                {
                    model.Description = row["Description"].ToString();
                }
                if (row["AddTime"] != null && row["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(row["AddTime"].ToString());
                }
                if (row["UpdateTime"] != null && row["UpdateTime"].ToString() != "")
                {
                    model.UpdateTime = DateTime.Parse(row["UpdateTime"].ToString());
                }
                if (row["CoverId"] != null && row["CoverId"].ToString() != "")
                {
                    model.CoverId = int.Parse(row["CoverId"].ToString());
                }
                if (row["ClassId"] != null && row["ClassId"].ToString() != "")
                {
                    model.ClassId = int.Parse(row["ClassId"].ToString());
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
                if (row["SortId"] != null && row["SortId"].ToString() != "")
                {
                    model.SortId = int.Parse(row["SortId"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AlbumId,Title,Description,AddTime,UpdateTime,CoverId,ClassId,UserId,SortId ");
            strSql.Append(" FROM picalbum ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM picalbum ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AlbumId desc");
            }
            strSql.Append(")AS Row, T.*  from picalbum T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "picalbum";
			parameters[1].Value = "AlbumId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 增加一条数据 并获取插入的ID值
        /// </summary>
        public int Add(Beauty.Model.picalbum model, string flag)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into picalbum(");
            strSql.Append("Title,Description,AddTime,UpdateTime,CoverId,ClassId,UserId,SortId)");
            strSql.Append(" values (");
            strSql.Append("@Title,@Description,@AddTime,@UpdateTime,@CoverId,@ClassId,@UserId,@SortId); SELECT @@identity");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Title", MySqlDbType.VarChar,255),
                    new MySqlParameter("@Description", MySqlDbType.VarChar,255),
                    new MySqlParameter("@AddTime", MySqlDbType.DateTime),
                    new MySqlParameter("@UpdateTime", MySqlDbType.DateTime),
                    new MySqlParameter("@CoverId", MySqlDbType.Int32,11),
                    new MySqlParameter("@ClassId", MySqlDbType.Int32,11),
                    new MySqlParameter("@UserId", MySqlDbType.Int32,11),
                    new MySqlParameter("@SortId", MySqlDbType.Int32,11)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.Description;
            parameters[2].Value = model.AddTime;
            parameters[3].Value = model.UpdateTime;
            parameters[4].Value = model.CoverId;
            parameters[5].Value = model.ClassId;
            parameters[6].Value = model.UserId;
            parameters[7].Value = model.SortId;

            int rows = Convert.ToInt32(DbHelperMySQL.GetSingle(strSql.ToString(), parameters));
            return rows;
        }


        /// <summary>
        /// 更新套图封面
        /// </summary>
        public bool Update(int albumId, int coverId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update picalbum set ");
            strSql.Append("CoverId=@CoverId,");
            strSql.Append("UpdateTime=@UpdateTime");
            strSql.Append(" where AlbumId=@AlbumId");

            MySqlParameter[] parameters = {
                    new MySqlParameter("@CoverId", MySqlDbType.Int32,11),
                    new MySqlParameter("@AlbumId", MySqlDbType.Int32,11),
                    new MySqlParameter("@UpdateTime", MySqlDbType.DateTime)
            };
            parameters[0].Value = coverId;
            parameters[1].Value = albumId;
            parameters[2].Value = DateTime.Now;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  ExtensionMethod
    }
}

